import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

public class Control implements ActionListener {

	Modell model;
	View view;
	Thread importer;
	
	public Control() {
		model =new Modell();
		importer=new Thread(model);
		view =new View(this);
		
		view.setVisible(true);
		
		model.initialize();
		
		view.setRooms(model.getOptions());
		
		view.setStats(model.getStats());
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		if(arg0.getActionCommand().compareTo("comboBoxChanged")==0)
			view.setRoompath(((JComboBox)arg0.getSource()).getSelectedItem().toString());
		else if(arg0.getActionCommand().compareTo("Start Import!")==0)
			importer.run();
		else if(arg0.getActionCommand().compareTo("Add new")==0){
			PokerFileChooser pfc= new PokerFileChooser(model.getAvailableRooms());
			pfc.showOpenDialog(null);
			while(pfc.getReturnValue()==0);
			if(pfc.getReturnValue()==1){
				pfc.setVisible(false);
				Map<String,String> temp=pfc.getSelected();
				String s=temp.keySet().toString().substring(1, temp.keySet().toString().length()-1);
				view.getRooms().put(s,temp.get(s));
				view.getRoomlist().addItem(s);
				model.getOptions().put(s,temp.get(s));
				model.save();
				model.CreateRoomTable(s);
			}
		}
		else if(arg0.getActionCommand().compareTo("Refresh")==0){
			view.setStats(model.getStats());
		}
	}

}
